//
//  LSRSeeBigPictureController.m
//  BaiSiProject
//
//  Created by lshenrong on 16/7/17.
//  Copyright © 2016年 lshenrong. All rights reserved.
//

#import "LSRSeeBigPictureController.h"
#import <UIImageView+WebCache.h>
#import "LSRThemeItem.h"
#import <Photos/Photos.h>
#import"XMGPhotoManger.h"
#import <SVProgressHUD.h>

@interface LSRSeeBigPictureController ()
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property(weak,nonatomic)UIImageView *pictureView;
@end

@implementation LSRSeeBigPictureController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    UIImageView *pictureView=[[UIImageView alloc] init];
//   服务器数据问题，要用image1来加载数据点击大图才会有动态图效果，image0虽然从模型数据里面看也是动态数据url，但是断点从模型里面复制url一复制到浏览器就可以看出服务器返回的不是动态图数据
    self.pictureView=pictureView;
    
    [pictureView sd_setImageWithURL:[NSURL URLWithString:_item.image1]];
    
    [self.scrollView addSubview:pictureView];
    
    CGFloat w=LSRScreenW;
    CGFloat h=LSRScreenW/self.item.width*_item.height;
    
    pictureView.frame=CGRectMake(0, 0, w, h);
    
    self.scrollView.contentSize=CGSizeMake(w, h);
    
    if (_item.isBigPicture==0) {
        
        pictureView.center=CGPointMake(LSRScreenW*0.5, LSRScreenH*0.5);
    }
    else
    {
       //编程image1,看大图更清晰
      [pictureView sd_setImageWithURL:[NSURL URLWithString:_item.image1]];
    
    
    }
    
}


- (IBAction)returnButton:(id)sender {
    
    [self dismissModalViewControllerAnimated:YES];
    
}

- (IBAction)saveButton:(id)sender {
//    
//    //写入到相册的方法,学习一个新的方法多看头文档，方法里面需要调用一个SEL方法一般都有固定的格式，参数self标识方法属于哪个对象的
//    // Adds a photo to the saved photos album.  The optional completionSelector should have the form:
//    //  - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
//    UIImageWriteToSavedPhotosAlbum(self.pictureView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    
    //保存照片到自定义的照片
    switch ([PHPhotoLibrary authorizationStatus]) {
        case  PHAuthorizationStatusNotDetermined ://不确定的情况会弹框
        {
          //弹框询问
           [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
               if (status==PHAuthorizationStatusAuthorized  ) {
                   
                 //保存图片
                   [XMGPhotoManger savePhoto:self.pictureView.image albumTitle:@"百思不得姐" completionHandler:^(BOOL success, NSError *error) {
                       
                       if (success)
                       {
                           
                           [SVProgressHUD showSuccessWithStatus:@"保存成功"];
                           
                       }
                       if (error)
                       {
                             [SVProgressHUD showErrorWithStatus:@"保存失败"];
                       }
                       
                       
                       
                   }];
                   
               }
           }];
            break;
        }
       case PHAuthorizationStatusAuthorized:
        {
        
//            /保存图片
            [XMGPhotoManger savePhoto:self.pictureView.image albumTitle:@"百思不得姐" completionHandler:^(BOOL success, NSError *error) {
                
                if (success)
                {
                    
                    [SVProgressHUD showSuccessWithStatus:@"保存成功"];
                    
                }
                if (error)
                {
                    [SVProgressHUD showErrorWithStatus:@"保存失败"];
                }
                
                
                
            }];

        
        
            break;
        
        }
            
            
        default:
            
        {
            //用户jujje
            //弹框告诉 用户需要允许访问相册
            [SVProgressHUD showInfoWithStatus:@"设置 -> 百思不得姐 -> 打开允许访问相册开关"];
        
        
        
        }
            break;
    }
    
    
    
    
    
    
    
    
    
    
    
    
    

    
}
//- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
//{
//    if (error) {
//        NSLog(@"保存出错");
//
//    }else
//    {
//       NSLog(@"保存成功");
//    
//    }
//
//}



@end
